package com.alibaba.algo.domain.user;

import com.alibaba.algo.cooperation.feign.vo.req.SelectOrgUserEmployeeVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectParentOrgReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectRoleUserReqVO;
import com.alibaba.algo.core.base.BaseDomain;
import com.alibaba.algo.core.base.BaseEntity;
import com.alibaba.algo.dao.user.SysOrgMapper;
import com.alibaba.algo.dtos.user.SysOrgDTO;
import com.alibaba.algo.entity.user.SysOrg;
import com.alibaba.algo.entity.user.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类SysOrg.java的实现描述：组织机构Domain
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Component
public class SysOrgDomain extends BaseDomain<SysOrg> {

    @Resource
    private SysOrgMapper sysOrgMapper;

    /**
     * 根据组织名称查询组织详情
     * 因为组织名称是唯一的,所以查询可行
     *
     * @param orgName 组织名称
     * @return 组织信息
     */
    public SysOrg qryOrgByOrgName(String orgName) {
        return sysOrgMapper.qryOrgByOrgName(orgName.trim());
    }

    /**
     * 查询组织下的用户列表
     *
     * @param orgId 组织标识
     * @return 用户列表
     */
    public List<SysUser> selectUserListByOrgId(Long orgId, String isDeleted) {
        return sysOrgMapper.selectUserListByOrgId(orgId, isDeleted);
    }

    /**
     * 模糊查询组织下的用户列表
     *
     * @param orgId     组织标识
     * @param searchKey 模糊匹配条件
     * @return 用户列表
     */
    public List<SysUser> selectUserListByOrgIdAndSearchKey(Long orgId, String searchKey, String isDeleted) {
        return sysOrgMapper.selectUserListByOrgIdAndSearchKey(orgId, searchKey, isDeleted);
    }


    /**
     * 查询当前组织及所有下级组织的组织列表
     *
     * @param orgId 组织标识
     * @return 组织列表
     */
    public List<Long> qryOrgIdListByOrgId(String orgId) {
        return sysOrgMapper.qryOrgIdListByOrgId(orgId);
    }


    public List<SysOrg> qryChildListByOrgId(String orgId) {
        return sysOrgMapper.qryChildListByOrgId(orgId);
    }


    /**
     * 根据父级组织id查询下级组织全量列表
     *
     * @param parentId 父级组织id
     * @param sortMap  排序规则
     * @return 组织列表
     */
    public List<SysOrg> qryOrgListByParentId(Long parentId, LinkedHashMap<String, Object> sortMap) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setParentId(parentId);
        return this.listByPara(sysOrg, sortMap);
    }

    public List<SysOrg> qryOrgListByParentIdAndDepartment(SelectParentOrgReqVO qrySelectParentOrgReqVO) {
        return sysOrgMapper.qryOrgListByParentIdAndDepartment(qrySelectParentOrgReqVO);
    }

    /**
     * 通过组织列表查询组织信息
     *
     * @param orgIdList 组织列表
     * @return 组织集合
     */
    public Map<Long, SysOrg> qryOrgMapByIdList(List<Long> orgIdList) {
        if (CollectionUtils.isEmpty(orgIdList)) {
            return Collections.emptyMap();
        }
        SysOrg org = new SysOrg();
        org.setOrgIdList(orgIdList);
        List<SysOrg> orgList = sysOrgMapper.listByPara(org, null);
        return orgList.stream().collect(Collectors.toMap(BaseEntity::getId, k -> k, (v1, v2) -> v2));
    }

    /**
     * 根据员工code集合查询员工code和用户id的对应关系map集合
     *
     * @param orgCodeList 组织列表
     * @return 组织集合
     */
    public Map<String, SysOrg> qryOrgMapByCodeList(List<String> orgCodeList) {
        if (CollectionUtils.isEmpty(orgCodeList)) {
            return Collections.emptyMap();
        }
        SysOrg org = new SysOrg();
        org.setOrgCodeList(orgCodeList);
        List<SysOrg> orgList = sysOrgMapper.listByParaWithIsDeleted(org, null);
        return orgList.stream().filter(item -> StringUtils.isNotEmpty(item.getOrgCode())).collect(Collectors.toMap(SysOrg::getOrgCode, k -> k, (v1, v2) -> v2));
    }

    /**
     * 通过组织id列表查询组织信息(支持查全量或有效)
     *
     * @param orgIdList 组织列表
     * @return 组织集合
     */
    public Map<Long, SysOrg> qryOrgMapWithIsDeletedByIdList(List<Long> orgIdList) {
        if (CollectionUtils.isEmpty(orgIdList)) {
            return Collections.emptyMap();
        }
        SysOrg org = new SysOrg();
        org.setOrgIdList(orgIdList);
        List<SysOrg> orgList = sysOrgMapper.listByParaWithIsDeleted(org, null);
        return orgList.stream().collect(Collectors.toMap(BaseEntity::getId, k -> k, (v1, v2) -> v2));
    }

    public SysOrg selectByOrgCode(String orgCode) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setOrgCode(orgCode);
        List<SysOrg> list = listByPara(sysOrg, null);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public SysOrg selectByOrgCodeWithIsDeleted(String orgCode) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setOrgCode(orgCode);
        List<SysOrg> list = sysOrgMapper.listByParaWithIsDeleted(sysOrg, null);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public SysOrg selectByOrgId(Long id) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setId(id);
        List<SysOrg> list = listByPara(sysOrg, null);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public SysOrg selectByOrgIdDefaultAll(Long id, String isDeleted) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setId(id);
        sysOrg.setIsDeleted(isDeleted);
        List<SysOrg> list = sysOrgMapper.listByParaWithIsDeleted(sysOrg, null);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据组织编码查询组织详情
     * 组织编码是唯一
     *
     * @param orgCode 组织名称
     * @return 组织信息
     */
    public SysOrg qryOrgByOrgCode(String orgCode) {
        return sysOrgMapper.qryOrgByOrgCode(orgCode);
    }

    public List<SysOrg> qryOrgListByOrgCode(String orgCode) {
        return sysOrgMapper.qryOrgListByOrgCode(orgCode);
    }

    public List<SysOrg> qryOrgListByIdsAndCodes(SelectRoleUserReqVO vo) {
        if (null != vo && !(ObjectUtils.isEmpty(vo.getOrgCodeList()) && ObjectUtils.isEmpty(vo.getOrgIds()))) {
            return sysOrgMapper.qryOrgListByIdsAndCodes(vo);
        } else {
            return Collections.emptyList();
        }
    }

    public int deleteOrgById(Long id) {
        return sysOrgMapper.deleteOrgById(id);
    }

    /**
     * 根据组织id向上查询与员工组相关的所有组织id
     *
     * @param id
     * @return
     */
    public List<Long> selectOrgIdListRecursiveToRootCorrelateGroup(Long id) {
        return sysOrgMapper.selectOrgIdListRecursiveToRootCorrelateGroup(id);
    }

    /**
     * 根据组织id向上查找所有组织id
     *
     * @param id
     * @return
     */
    public List<SysOrg> selectOrgIdListRecursiveToRoot(Long id) {
        return sysOrgMapper.selectOrgIdListRecursiveToRoot(id);
    }

    public Integer countBySort(Integer sort) {
        SysOrg sysOrg = new SysOrg();
        sysOrg.setSort(sort);
        Integer count = sysOrgMapper.countByPara(sysOrg);
        return count;
    }

    public List<SysOrgDTO> selectOrgByEmployeeCondition(SelectOrgUserEmployeeVO vo) {
        return sysOrgMapper.selectOrgByEmployeeCondition(vo);
    }

    public List<SysOrgDTO> selectOrgListByEmployeeCodesAndOrgIds(List<String> employeeCodeList, List<Long> orgIds, Integer matchFlag) {
        return sysOrgMapper.selectOrgListByEmployeeCodesAndOrgIds(employeeCodeList, orgIds, matchFlag);
    }

    public List<SysOrgDTO> selectOrgByEmployeePreciseCondition(SelectOrgUserEmployeeVO vo) {
        return sysOrgMapper.selectOrgByEmployeePreciseCondition(vo);
    }

    public List<SysOrgDTO> selectOrgByEmployeeAndOrgRangeCondition(SelectOrgUserEmployeeVO vo) {
        return sysOrgMapper.selectOrgByEmployeeAndOrgRangeCondition(vo);
    }

    public List<SysOrgDTO> selectOrgByOrgCondition(SelectOrgUserEmployeeVO vo) {
        return sysOrgMapper.selectOrgByOrgCondition(vo);
    }

    public SysOrg selectOneByCondition(SysOrg org) {
        return sysOrgMapper.selectOneByCondition(org);
    }

    public Integer deleteById(Long orgId) {
        return sysOrgMapper.deleteById(orgId);
    }

    public List<SysOrg> listByParaWithIsDeleted(SysOrg orgParam) {
        return sysOrgMapper.listByParaWithIsDeleted(orgParam, null);
    }

    public List<SysOrg> selectByOrgParentId(Long parentId) {
        SelectParentOrgReqVO selectParentOrgReqVO=new SelectParentOrgReqVO();
        selectParentOrgReqVO.setParentOrgId(parentId);
        return sysOrgMapper.qryOrgListByParentIdAndDepartment(selectParentOrgReqVO);
    }
}
